<template>
	<view class="LJH_mains">

		<u-form :model="model" class="start" ref="uForm" :errorType="errorType">
			<u-row>
				<u-col span="10" offset="1">
					<u-form-item :leftIconStyle="{color: '#888', fontSize: '32rpx'}" left-icon="account" label-width="150"
					 :label-position="labelPosition" label="姓名" prop="name">
						<u-input :border="border" placeholder="请输店您的姓名" type="text" v-model="model.name"></u-input>
					</u-form-item>
				</u-col>
			</u-row>
			<u-row>
				<u-col span="10" offset="1">
					<u-form-item :rightIconStyle="{color: '#888', fontSize: '32rpx'}" right-icon="kefu-ermai" :label-position="labelPosition"
					 label="手机号码" prop="phone" label-width="150">
						<u-input :border="border" placeholder="请输入您的手机号" type="number" v-model="model.phone"></u-input>
					</u-form-item>
				</u-col>
			</u-row>
			<u-row>
				<u-col span="10" offset="1">
					<u-form-item label="证件" :label-position="auto-upload" :error-type="errorType" label-width="500upx">
						<u-upload ref="uUpload" :action="action" show-tips="false" :show-progress="false" :auto-upload="true" :max-count="1"
						 :header="header" :name="name" @on-uploaded="onUploaded" size-type="compressed"></u-upload>
					</u-form-item>
				</u-col>
			</u-row>
		</u-form>

		<SearchCode></SearchCode>
		<u-toast ref="uToast" />
		<u-top-tips ref="uTips"></u-top-tips>

		<u-row class="LJH_end">
			<u-col span="6" offset="3">
				<button open-type="getUserInfo" hover-class='none' @getuserinfo="getUserInfoFun">授权并完善信息</button>
			</u-col>
		</u-row>
	</view>
</template>

<script>
	import SearchCode from "../../component/SearchQR.vue"
	export default {
		components: {
			SearchCode
		},
		data() {
			let that = this;
			return {
				userlist:{},
				errorType: ['message'],
				model: {
					name: '',
					intro: '',
					phone: '',
				},
				cust_agent_id:"",
				action: "",
				name: "img",
				rules: {
					name: [{
						required: true,
						message: '请输入姓名',
						// 可以单个或者同时写两个触发验证方式 
						trigger: ['change', 'blur'],
					}],
					intro: [{
							required: true,
							message: '请填写简介'
						},
						{
							min: 5,
							message: '简介不能少于5个字',
							trigger: 'change',
						},
						{
							pattern: /^[\u4e00-\u9fa5]+$/gi,
							message: '简介只能为中文',
							trigger: 'change',
						},
					],
					phone: [{
							required: true,
							message: '请输入手机号',
							trigger: ['change', 'blur'],
						},
						{
							validator: (rule, value, callback) => {
								// 调用uView自带的js验证规则，详见：https://www.uviewui.com/js/test.html
								return this.$u.test.mobile(value);
							},
							message: '手机号码不正确',
							// 触发器可以同时用blur和change，二者之间用英文逗号隔开
							trigger: ['change', 'blur'],
						}
					]
				},
			};
		},
		computed: {},
		onReady() {
			this.$refs.uTips.show({
				title: '欢迎您尊敬的客户',
				type: 'success',
				duration: '2300'
			})
			this.uUpload = this.$refs.uUpload
			this.action = this.$url + "/index.php/api/v1.Tool/uploadFlie"
			this.$refs.uForm.setRules(this.rules);
		},
		methods: {
			loginsuccess() {
				this.$refs.uToast.show({
					title: '提交成功',
					type: 'success',
					icon: "default",
					// url: '/pages/shop/addshop'
				})
			},
			// loginfail(x){
			// 		this.$refs.uToast.show({
			// 			title: x,
			// 			type: 'warning',
			// 		})
			// 		this.fail=x
			// },
			login() {
				let that=this
				console.log(that.userlist.code);
				console.log(that.userlist.nickName);
				console.log(that.userlist.avatarUrl);
				console.log(that.cust_agent_id);
				that.$flyio.post('v1.Login/qrlogin', {
						code: that.userlist.code,
						wx_nick_name: that.userlist.nickName,
						wx_total:that.userlist.avatarUrl,
						agent_id:that.cust_agent_id
					}).then((ret) => {
						that.list = ret.msg
						console.log("提交的返回");
						console.log(ret)
						that.show = true
						that.$refs.uToast.show({
							title: ret.msg,
							type: 'success',
						})
						if(ret.msg=="成功"||ret.msg=="绑定成功"){
							setTimeout(uni.navigateTo({
								url:"../user/index"
							}),2000)
						}
						
					})
					.catch((e) => {
						console.log(e)
					})
			},
			Getcust_agent_id(){
				let that=this
				wx.getStorage({
					key:"cust_agent_id",
					success(res){
						that.cust_agent_id=res.data
						console.log("获得了绑定的业务员id"+that.cust_agent_id);
						that.login()
					}
				})
				
			},
			onUploaded(lists) {
				this.filesArr = lists;
				this.wanshanimg = this.filesArr[0].url
				// console.log(this.wanshanimg);
				console.log("上传中")
				console.log(this.wanshanimg);
			},
			submit() {
				var that = this
				console.log("要发送的数据")
				console.log(that.agent_id)
				console.log(that.id)
			},
			getUserInfoFun() {
				var that = this
				wx.login({
					success: function(res) {
						var code = res.code
						wx.getUserInfo({
							success: function(res) {
								var userInfo = res.userInfo
								var nickName = userInfo.nickName
								var avatarUrl = userInfo.avatarUrl
								var userlist = {
									nickName: nickName,
									avatarUrl: avatarUrl,
									code: code
								}
								that.userlist=userlist
								console.log(userlist);
								that.Getcust_agent_id()
							}
						})
					}
				});
			},
		}
	};
</script>

<style scoped lang="scss">
	.u-font-20 {
		color: #D3D3D3;
	}

	.buttons {
		border-radius: 10upx;
	}
</style>
